import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syria_sports/presentation/exercises/exercises_page/models/exercise.dart';

import '../../../core/utils/show_toast.dart';
import '../../../data/controllers/complaints_controller.dart';
import '../models/complaint.dart';

part 'user_complaints_state.dart';

class UserComplaintsCubit extends Cubit<UserComplaintsState> {
  UserComplaintsCubit() : super(UserComplaintsInitial());

  static UserComplaintsCubit get(context) => BlocProvider.of(context);


  List<Complaint> notes = [];
  void init() {
    getAllNotes();
  }
  Future getAllNotes()async{
    emit(UserComplaintsLoading());
    await ComplaintsController.getAllComplaints().catchError((e){
      emit(UserComplaintsError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        notes = List<Complaint>.generate(value.data.length, (index) => Complaint.fromJson(value.data[index]));
        notes= notes.reversed.toList();
        emit(UserComplaintsDone());
      }else if(value != null){
        showMessage(value.message);
        emit(UserComplaintsError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }


}
